package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Name DishController
 * @Author stella
 * @Date 2023-03-05 11:06 AM
 */
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 菜品列表
     * 分页条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 String name) { //注解@RequestParam接收的参数是来自requestHeader中，即请求头。
        // @RequestBody接收的参数是来自requestBody中，即请求体。
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);
        return ResultInfo.success(page);
    }

    /**
     * 新增菜品
     *
     * @param dish
     * @return
     */
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        dishService.save(dish);
        //清理缓存 ( 管理端新增了菜品 根据所在整个分类的key直接删除
        String dishKey = "dish_" + dish.getCategoryId();
        redisTemplate.delete(dishKey);
        return ResultInfo.success();
    }
    //抽取删除缓存的公共方法
    private void clean(String key) {
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }

    /**
     * 菜品修改：回显菜品详情
     *
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    /**
     * 修改菜品/口味
     *
     * @param dish
     * @return
     */
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        dishService.update(dish);

        // 清理缓存
        clean("dish_*");
        return ResultInfo.success();
    }

    /**
     * 批量启停售
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status,
                                   @RequestParam List<Long> ids) {
        dishService.updateStatus(status, ids);

        // 清理缓存
        clean("dish_*");
        return ResultInfo.success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchId(@RequestParam List<Long> ids) {
        dishService.deleteBatchId(ids);
        return ResultInfo.success();
    }

    /**
     * 回显菜品列表
     *
     * @param categoryId
     * @param name
     * @return
     */
    @GetMapping("/dish/list")
    public ResultInfo findList(Long categoryId, String name) {
        List<Dish> dishes = dishService.findList(categoryId, name);
        return ResultInfo.success(dishes);
    }
}
